# ui/hud/StatusBars.gd (节点脚本，挂载到HUD节点)
extends Control

# === 节点引用 ===
@onready var stamina_ring: TextureProgressBar = $StaminaRing
@onready var hunger_ring: TextureProgressBar = $HungerRing
@onready var thirst_ring: TextureProgressBar = $ThirstRing
@onready var heart_rate_ring: TextureProgressBar = $HeartRateRing
@onready var fatigue_ring: TextureProgressBar = $FatigueRing

# === 初始化 ===
func _ready() -> void:
	# 连接事件总线信号
	_EventBus.stamina_updated.connect(_on_stamina_updated)
	_EventBus.hunger_updated.connect(_on_hunger_updated)
	_EventBus.thirst_updated.connect(_on_thirst_updated)
	_EventBus.heart_rate_updated.connect(_on_heart_rate_updated)
	_EventBus.fatigue_updated.connect(_on_fatigue_updated)
	# 初始化UI状态
	_update_stamina_visual(_AttributeSystem.base_stamina)
	_update_hunger_visual(_AttributeSystem.base_hunger)
	_update_thirst_visual(_AttributeSystem.base_thirst)

# === 信号处理 ===
func _on_stamina_updated(value: float) -> void:
	_update_stamina_visual(value)

func _on_hunger_updated(value: float) -> void:
	_update_hunger_visual(value)

func _on_thirst_updated(value: float) -> void:
	_update_thirst_visual(value)

# === UI更新方法 ===
func _update_stamina_visual(value: float) -> void:
	"""更新体能环状图"""
	stamina_ring.value = value
	stamina_ring.tint_progress = _get_ring_color(value / 200.0)

func _update_hunger_visual(value: float) -> void:
	"""更新饥饿进度条"""
	hunger_ring.value = value
	hunger_ring.get_node("Label").text = "饥饿: %d%%" % value

func _update_thirst_visual(value: float) -> void:
	"""更新口渴进度条"""
	thirst_ring.value = value
	thirst_ring.get_node("Label").text = "口渴: %d%%" % value

# === 辅助方法 ===
func _get_ring_color(ratio: float) -> Color:
	"""根据比例返回颜色渐变（绿->黄->红）"""
	if ratio >= 0.5:
		return Color.GREEN.lerp(Color.YELLOW, (ratio - 0.5) * 2)
	else:
		return Color.YELLOW.lerp(Color.RED, ratio * 2)
		
func _on_heart_rate_updated(value: float) -> void:
	heart_rate_ring.value = value
	heart_rate_ring.tint_progress = _get_alert_color(value, 80.0, 180.0)

func _on_fatigue_updated(value: float) -> void:
	fatigue_ring.value = value
	fatigue_ring.get_node("Label").text = "疲劳: %d%%" % value

func _get_alert_color(current: float, min_safe: float, max_safe: float) -> Color:
	"""根据安全阈值返回警报色"""
	if current < min_safe:
		return Color.YELLOW
	elif current > max_safe:
		return Color.RED
	else:
		return Color.GREEN
